var Microblog = function() {
	var $component = null;
	var currentPage = 1;
	
	create();
	
	this.getComponent = function() {
		return $component;
	};
	
	function create() {
		$component = MicroblogTemplate.normal();
		attachListeners();
		
		load();
	};
	
	function load() {
		$("html").addClass("busy");
		
		var microblogContent = downloadMicroblogContent(currentPage);
		
		$component.find(".microblog_content").children().detach();
		loadMicroblogContent(microblogContent);
		$component.find(".current_page").text(currentPage);
		
		$("html").removeClass("busy");
	}
	
	function downloadMicroblogContent(page) {
		var microblogContent = null;
		var url = {
				type: "Stream",
				method: "Index",
				param: "page,"+page
		};
		var success = function(microblog) {
			microblogContent = microblog;
		};
		var error = function(e) {
			console.log("error");
			console.log(e);
		};
		
		var params = {
				url: url,
				success: success,
				error: error,
				sync: true
		};
		
		Request.send(params);
		
		return microblogContent;
	};
	
	function pageForward() {
		currentPage++;
		load();
	};
	
	function pageBackward() {
		if(currentPage > 1) {
			currentPage--;
		}
		load();
	};
	
	function loadMicroblogContent(microblog) {
		$.each(microblog, function(i, entry) {
			$component.find(".microblog_content").append(new Entry(entry, false).getComponent());
		});
	};
	
	function attachListeners() {
		attachPagerForwardListener();
		attachPagerBackwardListener();
		attachRefreshListener();
	};
	
	function attachPagerForwardListener() {
		$component.find(".page_forward").click(function() {
			pageForward(); 
		});
	};
	
	function attachPagerBackwardListener() {
		$component.find(".page_backward").click(function() {
			pageBackward();
		});
	};
	
	function attachRefreshListener() {
		$component.find(".microblog_refresh").click(function() {
			load();
		});
	};
};